package com.etl.borrow.service;

import com.etl.borrow.enums.BorrowStatus;
import com.etl.borrow.model.Borrow;
import com.etl.borrow.dao.IBorrowDao;
import com.etl.borrow.model.BorrowData;
import com.xr.enums.RepaymentMode;
import com.xr.service.impl.BaseServiceImpl;
import com.xr.util.Assert;
import com.xr.util.DateUtil;
import com.xr.util.ExceptionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 借款项目 服务实现类
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-03-24
 */
@Service("borrowService")
public class BorrowServiceImpl extends BaseServiceImpl<IBorrowDao, Borrow> implements IBorrowService {

  @Override
  public Long apply(Borrow borrowInfo) throws Exception {

    // 创建借款标的
    long now = DateUtil.currentTimeInSecond();
    borrowInfo.setVersion(0);
    borrowInfo.setCreateTime(now);
    borrowInfo.setUpdateTime(now);
    borrowInfo.setStatus(BorrowStatus.CHECK.getCode());
    borrowInfo.setRepaymentType(RepaymentMode.parse(borrowInfo.getRepaymentType()).getCode());
    borrowInfo = this.insert(borrowInfo);

    // 创建借款标的相关的记录信息
    BorrowData bd = new BorrowData();
    bd.setBorrowId(borrowInfo.getBorrowId());
    bd.setCreateTime(borrowInfo.getCreateTime());
    bd.setUpdateTime(borrowInfo.getUpdateTime());
    borrowDataService.insert(bd);

    return borrowInfo.getBorrowId();
  }

  @Override
  public void refuse(long borrowId, BorrowData borrowData) throws Exception {
    Borrow borrow = this.queryBorrow(borrowId);
    if(borrow.getStatus().intValue() > BorrowStatus.RECHECK.getCode()){
      // 复审过后不允许拒绝（只能流标）
      ExceptionUtil.throwBizException("当前标的不允许拒绝");
    }

    long now = DateUtil.currentTimeInSecond();

    borrowData.setBorrowId(borrow.getBorrowId());
    borrowData.setUpdateTime(now);
    borrowDataService.updateById(borrowData);

    Borrow updateBorrow = new Borrow();
    updateBorrow.setBorrowId(borrowId);
    updateBorrow.setVersion(borrow.getVersion());
    updateBorrow.setStatus(BorrowStatus.REFUSE.getCode());
    updateBorrow.setUpdateTime(now);
    if(this.updateById(updateBorrow).intValue() != 1){
      ExceptionUtil.throwBizException("更新标的信息失败，请稍候重试。");
    }
  }

  @Override
  public void check(long borrowId, BorrowData borrowData) throws Exception {
    Borrow borrow = this.queryBorrow(borrowId);
    if(borrow.getStatus().intValue() != BorrowStatus.CHECK.getCode()){
      ExceptionUtil.throwBizException("标的状态不合法");
    }

    long now = DateUtil.currentTimeInSecond();

    borrowData.setBorrowId(borrow.getBorrowId());
    borrowData.setUpdateTime(now);
    borrowDataService.updateById(borrowData);

    // 进入复审流程
    Borrow updateBorrow = new Borrow();
    updateBorrow.setBorrowId(borrowId);
    updateBorrow.setVersion(borrow.getVersion());
    updateBorrow.setStatus(BorrowStatus.RECHECK.getCode());
    updateBorrow.setUpdateTime(now);
    if(this.updateById(updateBorrow).intValue() != 1){
      ExceptionUtil.throwBizException("初审失败，请稍候重试。");
    }
  }

  @Override
  public void recheck(long borrowId, BorrowData borrowData) throws Exception {
    Borrow borrow = this.queryBorrow(borrowId);
    if(borrow.getStatus().intValue() != BorrowStatus.RECHECK.getCode()){
      ExceptionUtil.throwBizException("标的状态不合法");
    }

    long now = DateUtil.currentTimeInSecond();

    borrowData.setBorrowId(borrow.getBorrowId());
    borrowData.setUpdateTime(now);
    borrowDataService.updateById(borrowData);

    // 进入发标流程
    Borrow updateBorrow = new Borrow();
    updateBorrow.setBorrowId(borrowId);
    updateBorrow.setVersion(borrow.getVersion());
    updateBorrow.setStatus(BorrowStatus.CHECKED_BID.getCode());
    updateBorrow.setUpdateTime(now);
    if(this.updateById(updateBorrow).intValue() != 1){
      ExceptionUtil.throwBizException("复审失败，请稍候重试。");
    }
  }

  @Override
  public void release(long borrowId) throws Exception {
    Borrow borrow = this.queryBorrow(borrowId);
    if(borrow.getStatus().intValue() != BorrowStatus.CHECKED_BID.getCode()){
      ExceptionUtil.throwBizException("标的状态不合法");
    }

    // 进入投标流程
    Borrow updateBorrow = new Borrow();
    updateBorrow.setBorrowId(borrowId);
    updateBorrow.setVersion(borrow.getVersion());
    updateBorrow.setStatus(BorrowStatus.IN_BID.getCode());
    updateBorrow.setUpdateTime(DateUtil.currentTimeInSecond());
    if(this.updateById(updateBorrow).intValue() != 1){
      ExceptionUtil.throwBizException("发标失败，请稍候重试。");
    }
  }

  @Override
  public void fullBid(long borrowId) throws Exception {

  }

  private Borrow queryBorrow(long borrowId){
    Borrow borrow = this.selectById(borrowId);
    Assert.notNull(borrow, "标的不存在");
    return borrow;
  }

  @Resource
  private IBorrowDataService borrowDataService;
}
